import { defineStore } from "pinia";
import {useSessionStorage} from "@vueuse/core";

import {store} from "@/store/index.js";

// setup
export const useFlowStore = defineStore("flow", () => {
    // state
    const nodeMap = useSessionStorage("nodeMap", new Map());
    const isEdit = useSessionStorage("isEdit", null);
    const selectedNode = useSessionStorage("selectedNode", {});

    const selectFormItem = useSessionStorage("selectFormItem", null);
    const design = useSessionStorage("design", {
        formId: null,
        formName: "",
        formKey: "",
        formUrl: "",
        logo: {
            icon: "",
            background: ""
        },
        settings: {
            commiter: [],
            admin: [],
            sign: false,
            notify: {
                types: ["APP"],
                title: ""
            }
        },
        groupId: undefined,
        formItems: [],
        process: {
            id: "",
            parentId: null,
            type: "",
            name: "",
            desc: "",
            props: {
                assignedUser: [],
                formPerms: []
            },
            children: {}
        },
        remark: ""
    });

    // design: {
    //     formItems: [],
    //         process: {
    //         //节点ID，当前流程内唯一，由前端随机生成
    //         id: "root",
    //             parentId: "7d89sf7d8sasf", //父级节点ID，用来向上搜索
    //             //节点类型，ROOT(发起人，根节点)、APPROVAL(审批)、CC(抄送)、CONDITION(条件)、CONCURRENT（并行节点）、DELAY(延时节点)、EMPTY(空节点，占位)
    //             type: "ROOT",
    //             name: "发起人",
    //             props: {
    //             assignedUser: []
    //         }, //节点属性，见下方props说明
    //         children: {}, //子节点项，内部字段与当前结构相同，为条件/并行 节点的组合末端节点
    //     }
    // },
    // actions
    const selectedNodeChange = (val) => {
        selectedNode.value = val
    }

   const loadForm = (val) => {
        design.value = val
   }

   const setIsEdit = (val) => {
        isEdit.value = val
    }

    return {
        nodeMap,
        isEdit,
        selectedNode,
        selectFormItem,
        design,
        selectedNodeChange,
        loadForm,
        setIsEdit,
    };
},);

// 非setup
export function useFlowStoreHook() {
    return useFlowStore(store);
}
